package com.foreknow.controller;

import com.foreknow.po.Cart;
import com.foreknow.service.CartService;
import com.foreknow.service.Impl.CartServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class CartController {

    public Object listCart(HttpServletRequest request){
        CartService cartService = new CartServiceImpl();
        String userId = request.getParameter("userId");
        Integer businessId= null;
        if (request.getParameter("businessId") != null ){
            businessId= Integer.parseInt(request.getParameter("businessId"));
        }
        List<Cart> list =  cartService.listCart(userId,businessId);
        return list;
    }

    public Object saveCart(HttpServletRequest request){
        CartService cartService = new CartServiceImpl();
        String userId = request.getParameter("userId");
        Integer businessId = Integer.parseInt(request.getParameter("businessId"));
        Integer foodId = Integer.parseInt(request.getParameter("foodId"));
        Integer result = cartService.saveCart(userId,foodId,businessId);
        return result;
    }

    public Object updateCart(HttpServletRequest request){
        CartService cartService = new CartServiceImpl();
        String userId = request.getParameter("userId");
        Integer businessId = Integer.parseInt(request.getParameter("businessId"));
        Integer foodId = Integer.parseInt(request.getParameter("foodId"));
        Integer quantity = Integer.parseInt(request.getParameter("quantity"));
        Integer result = cartService.updateCart(userId,foodId,businessId,quantity);
        return result;
    }

    public Object removeCart(HttpServletRequest request){
        CartService cartService = new CartServiceImpl();
        String userId = request.getParameter("userId");
        Integer businessId = Integer.parseInt(request.getParameter("businessId"));
        Integer foodId = null;

        if (request.getParameter("foodId")!=null){
            foodId = Integer.parseInt(request.getParameter("foodId"));
        }

        Integer result = cartService.removeCart(userId,foodId,businessId);
        return result;
    }

}
